﻿using AutoMapper;
using ClothingShopping.Controllers.BaseControllers;
using ClothingShopping.Data;
using ClothingShopping.Dto;
using ClothingShopping.Dto.AccountDto;
using ClothingShopping.IRepository;
using ClothingShopping.IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Runtime.CompilerServices;

namespace ClothingShopping.Controllers
{
    public class AccountController : ProtectedController
    {
        #region Fields
        private readonly IUserService _userService;
        private readonly IMapper _mapper;
        #endregion

        #region Ctors
        public AccountController(IUserService userService, IMapper mapper)
        {
            _userService = userService;
            _mapper = mapper;
        }
        #endregion

        #region Methods
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IResultOutput> AddUserAsync([FromBody] User user)
        {
            var result= await _userService.AddUserAsync(user);
            return  result;
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<IResultOutput> DeleteUserAsync(int id)
        {
            var result = await _userService.DeleteUserAsync(id);
            return result;
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<IResultOutput> GetUserByIdAsync([FromServices]IMapper mapper, int id = 0, string? userNmae=null)
        {
            var result = await _userService.GetUserByIdAsync(id,userNmae);
            if(result == null)
            {
                return ResultOutput.Error("用户不存在");
            }
            var userDto=mapper.Map<UserDto>(result);
            return ResultOutput.Ok(userDto);
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="updateUserDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IResultOutput> UpdateUserAsync([FromBody] UserDto userDto)
        {
            var result = await _userService.UpdateUserAsync(userDto);
            return result;
        }
        #endregion
    }
}
